package red.social;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ObtenerPosicion extends MapActivity {
	
	MapView mapView;
	MapController mc;
	List<Overlay> mapOverlays;
	ItemizedOverlay itemizedOverlay;
	private Drawable drawable;
	Bundle parametros;
	String ip;
	private Timer timer = null;
	private String idusuario;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapa);
		ip = getResources().getString(R.string.ip);
		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(true);

		mc = mapView.getController();
		mc.setZoom(16);
		mapOverlays = mapView.getOverlays();
		if (!mapOverlays.isEmpty()) {
			mapOverlays.clear();
		}
	
		parametros = getIntent().getExtras();
		idusuario = parametros.getString("idamigo");

		try {
			// Creamos el timer
			this.timer = new Timer();

			// Configuramos lo que tiene que hacer
			this.timer.scheduleAtFixedRate(new TimerTask() {
				public void run() {
					obtenerPosicion();
				}
			}, 0, 5000);

			
		} catch (Exception e) {
			Log.i(getClass().getSimpleName(), e.getMessage());
		}
	}

	private void obtenerPosicion() {
		try {
			String servletURL = "http://" + ip
					+ ":8080/Modulo10-war/ObtenerPosicion";
			URL ob = new URL(servletURL + "?idusuario=" + idusuario);
			URLConnection conn = ob.openConnection();

			conn.setDoInput(true);
			conn.setDoOutput(true);
			InputStream in = conn.getInputStream();
			DataInputStream dos = new DataInputStream(in);
			String lon = dos.readLine();
			String lat = dos.readLine();

			if ((lat == null) || (lon == null)) {
				CharSequence text = "No se encuentra la posicion del amigo solicitado!";
				int duration = Toast.LENGTH_LONG;

				Toast toast = Toast.makeText(ObtenerPosicion.this, text, duration);
				toast.show();

			} else {

				double plat = Double.parseDouble(lat);
				double plon = Double.parseDouble(lon);
				int latint = (int)plat;
				int lonint = (int)plon;
				drawable = getResources().getDrawable(R.drawable.androidmarker);
				itemizedOverlay = new ItemizedOverlay(drawable);
				GeoPoint point = new GeoPoint(latint, lonint);
				OverlayItem overlayitemUsuario = new OverlayItem(point, "", "");
				itemizedOverlay.addOverlay(overlayitemUsuario);
				mapOverlays.add(itemizedOverlay);
				mc.setCenter(point);
			}
			dos.close();
			conn = null;

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
